<template>
  <div>
    <el-card class="card">
      <div slot="header">
        <span>商品添加</span>
      </div>
      <!-- 表单 -->
      <el-form ref="form" :model="formData" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="formData.name" placeholder="商品名称"></el-input>
        </el-form-item>
        <!-- 商品分类 -->
        <el-form-item label="商品分类">
          <el-select v-model="formData.category" placeholder="请选择商品分类">
            <el-option
              v-for="(item,index) in allCates"
              :key="index"
              :value="item"
              >{{ item }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格">
          <!-- <el-col class="line" :span="2">-</el-col> -->
          <el-input-number
            v-model="formData.price"
            :min="1"
            :max="10"
            label="描述文字"
          >
          </el-input-number>
        </el-form-item>
        <!-- 商品图片 -->
        <div class="cap df">
          <span>商品图片</span>
          <el-upload
            class="avatar-uploader"
            :action="`${API}/users/avatar_upload`"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="formData.imgUrl" :src="formData.imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <el-form-item label="商品描述">
          <el-input type="textarea" v-model="formData.goodsDesc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">添加商品</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 引入商品添加接口函数
import { goodsCategoriesReq, goodsAddReq } from "@/api/goods";
export default {
  data() {
    return {
      API: "http://sell.h5.itsource.cn:8087",
      imageUrl: "",
      formData: {
        name: "", //商品名称
        category: "", //商品所属分类
        price: "", //商品价格
        imgUrl: "", //图片地址
        goodsDesc: "", //商品描述
      
       
      },
      allCates: [], //所有分类
    };
  },
  methods: {
    // handleChange(value) {
    //   console.log(value);
    // },
    async onSubmit() {
      // 发送请求
      console.log(this.formData);
      let res = await goodsAddReq(this.formData);
      //    console.log(this.formData);
      // 解构数据
      let { code, msg } = res.data;
      console.log(code);
      // 判断
      if (code === 0) {
        this.$router.push("/goods/goods-list");
      }
    },
    handleAvatarSuccess(res) {
      console.log(res);
      let {imgUrl}=res
      this.formData.imgUrl=imgUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    // 初始化发请求
    async getAllCates() {
      let res = await goodsCategoriesReq();
      console.log('res.data',res.data);
      // 数据赋值
      this.allCates = res.data;
    },
  },

  created() {
    this.getAllCates();
  },
};
</script>

<style lang="scss" scoped>
.cap {
  margin-left: 12px;
}

.form {
  width: 285px;
}

.avatar-uploader :deep(.el-upload) {
  margin-left: 12px;
  margin-bottom: 10px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
</style>